package cn.b.sky.mes.cm.service.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.b.sky.mes.cm.dao.CapacityCoefficientDao;
import cn.b.sky.mes.cm.dao.StandardCapacityDao;
import cn.b.sky.mes.cm.model.CapacityCoefficient;
import cn.b.sky.mes.cm.model.StandardCapacity;
import cn.b.sky.mes.cm.model.condition.StandardCapacityCondition;
import cn.b.sky.mes.cm.service.StandardCapacityService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("standardCapacityService")
public class StandardCapacityServiceImpl implements StandardCapacityService {

  private static final Logger LOG = LoggerFactory.getLogger(StandardCapacityServiceImpl.class);

  @Resource
  StandardCapacityDao standardCapacityDao;

  @Resource
  CapacityCoefficientDao capacityCoefficientDao;

  @Override
  public List<StandardCapacity> selectStandardCapacityListByCondition(StandardCapacityCondition condition) {
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return standardCapacityDao.selectStandardCapacityListByCondition(condition);
  }

  @Override
  public int selectStandardCapacityCountByCondition(StandardCapacityCondition condition) {
    return standardCapacityDao.selectStandardCapacityCountByCondition(condition);
  }

  @Override
  public StandardCapacity getStandardCapacity(String workCenterCode, String materialCode, String siteCode) {
    if (StringUtils.isBlank(workCenterCode) || StringUtils.isBlank(materialCode) || StringUtils.isBlank(siteCode)) {
      return null;
    }
    StandardCapacity standardCapacity = standardCapacityDao.getStandardCapacity(workCenterCode, materialCode, siteCode);
    if (standardCapacity != null) {
      List<CapacityCoefficient> coefficients = capacityCoefficientDao.selectCapacityCoefficientList(workCenterCode,
          materialCode, siteCode);
      Collections.sort(coefficients);
      standardCapacity.setCoefficients(coefficients);
    }

    return standardCapacity;
  }

  @Override
  public void updateStandardCapacity(StandardCapacity standardCapacity) {
    if (StringUtils.isBlank(standardCapacity.getWorkCenterCode())
        || StringUtils.isBlank(standardCapacity.getMaterialCode())
        || StringUtils.isBlank(standardCapacity.getSiteCode())) {
      return;
    }
    standardCapacityDao.updateStandardCapacity(standardCapacity);
    capacityCoefficientDao.deleteAllCapacityCoefficient(standardCapacity.getWorkCenterCode(),
        standardCapacity.getMaterialCode(), standardCapacity.getSiteCode());
    this.insertCoefficients(standardCapacity);
  }

  @Override
  public BatchJobReport deleteStandardCapacity(String workCenterCode, String materialCode, String siteCode) {
    if (!StringUtils.isBlank(workCenterCode) && !StringUtils.isBlank(siteCode)) {
      String message = workCenterCode;
      String cause = "";
      boolean success = true;
      try {
        capacityCoefficientDao.deleteAllCapacityCoefficient(workCenterCode, materialCode, siteCode);
        standardCapacityDao.deleteStandardCapacity(workCenterCode, materialCode, siteCode);
      } catch (Exception e) {
        LOG.error("{}", e);
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(errorMsg)) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(),
              "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
    } else {
      return new BatchJobReport("", "id is null", false, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
    }
  }

  @Override
  public void insertStandardCapacity(StandardCapacity standardCapacity) {
    if (StringUtils.isBlank(standardCapacity.getWorkCenterCode())
        || StringUtils.isBlank(standardCapacity.getMaterialCode())
        || StringUtils.isBlank(standardCapacity.getSiteCode())) {
      return;
    }
    standardCapacityDao.insertStandardCapacity(standardCapacity);
    if (standardCapacity.getCoefficients().size() > 0) {
      this.insertCoefficients(standardCapacity);
    }
  }

  @Override
  public void insertCoefficients(StandardCapacity standardCapacity) {
    List<CapacityCoefficient> coefficients = standardCapacity.getCoefficients();
    String siteCode = standardCapacity.getSiteCode();
    String workCenterCode = standardCapacity.getWorkCenterCode();
    String materialCode = standardCapacity.getMaterialCode();
    /*  if(StringUtils.isBlank(workCenterCode)||StringUtils.isBlank(materialCode)||StringUtils.isBlank(siteCode))
      {
        return;
      }*/
    int ordinal = 0;
    for (CapacityCoefficient coefficient : coefficients) {
      if (coefficient.getLowerQuantity() != null || coefficient.getUpperQuantity() != null) {
        coefficient.setOrdinal(ordinal++);
        coefficient.setSiteCode(siteCode);
        coefficient.setWorkCenterCode(workCenterCode);
        coefficient.setMaterialCode(materialCode);
        capacityCoefficientDao.insertCapacityCoefficient(coefficient);
      }
    }
  }

  @Override
  public List<StandardCapacity> selectAllStandardCapacityList(String siteCode, String workCenterCode) {
    return standardCapacityDao.selectAllStandardCapacityList(siteCode, workCenterCode);
  }

  @Override
  public StandardCapacity isExsitsStandardCapacity(String workCenterCode, String materialCode, String siteCode) {
    if (StringUtils.isBlank(workCenterCode) || StringUtils.isBlank(materialCode) || StringUtils.isBlank(siteCode)) {
      return null;
    }
    return standardCapacityDao.getStandardCapacity(workCenterCode, materialCode, siteCode);
  }

}
